package com.sudichina.sudichina.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.sudichina.sudichina.utils.L;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by ${杨伟乔} on 2017/3/19.
 * 邮箱: yangweiqiao@126.com
 * QQ: 463217558
 * 别人光鲜的背后或者有着太多不为人知的痛苦
 * 学习可以改变一切、 行动可以解决一切问题、
 */

public class MyReceiver extends BroadcastReceiver {
    private final static String TAG = MyReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (TextUtils.isEmpty(action)) {
            L.e(TAG, "action=" + action);
            return;
        }

        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(action)) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            return;
        }

        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(action)) {
            L.e(TAG, "[JpushReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
//            processCustomMessage(context, bundle);
            return;
        }

        if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(action)) {
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            L.e(TAG, "[JpushReceiver] 接收到推送下来的通知的ID: " + notifactionId);

            String alert = bundle.getString(JPushInterface.EXTRA_ALERT);
            if (TextUtils.isEmpty(alert)) {
                return;
            }
            L.e(TAG, "alert===" + alert);

            //消息附加字段
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (TextUtils.isEmpty(extra)) {
                return;
            }
            L.e(TAG, "extra===" + extra);

            return;
        }
        if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) {
            L.e(TAG, "[JpushReceiver] 用户点击打开了通知");
            openNotification(context, bundle);
            return;
        }
        if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(action)) {
            L.e(TAG, bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..
        }
        if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(action)) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            L.e(TAG, "[JpushReceiver]" + action + " connected state change to " + connected);
        }
    }
    private void openNotification(Context context, Bundle bundle) {
        L.e(TAG, "--------openNotification-----");
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Gson gson = new Gson();

        System.out.println("extras"+extras);
//        PushBean bean = gson.fromJson(extras, PushBean.class);
         BaseActivity.gotoMainActivity(context,null);



    }
}
